<template>
    <div>
        <div class="header-container" v-if="isShowHeader">
            <div class="logo-box" @click="logoFn">
                <img src="../assets/img/logo.png" alt="">
                <span>友牧农场后台管理系统</span>
            </div>
            <div class="user-box">
                <div class="user-name">
                    <i class="el-icon-user-solid"></i>
                    <span>{{nickName}}</span>
                </div>
                <div class="user-identity">
                    <el-dropdown @command="commandFn">
                        <span class="el-dropdown-link">
                            {{roleName}}<i class="el-icon-arrow-down el-icon-caret-bottom"></i>
                        </span>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item icon="el-icon-user-solid" command="1">个人信息</el-dropdown-item>
                            <el-dropdown-item icon="el-icon-edit" command="2">修改手机号</el-dropdown-item>
                            <el-dropdown-item icon="el-icon-switch-button"  command="3">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import Storage from "@/utils/storage";
export default {
    inject: ['reload'],
    data() {
        return {
            isShowHeader: false,
            roleName: '',
            nickName: ''
        }
    },
    created() {
        this.nickName = Storage.get('nickName') || ''
        this.roleName = Storage.get('roleName') || ''
    },
    computed:{
        getLoginState() {
            return this.$store.state.user.loginState
        }
    },
    watch: {
        $route(to, from) {
            let bottomPage = ['login'];
            let toPageName = to.name;
            let isNeedShowHeader = true;
            for (let i=0; i<bottomPage.length; i++) {
                if (toPageName === bottomPage[i]) {
                    isNeedShowHeader = false;
                    break;
                }
            }
            this.isShowHeader = isNeedShowHeader;
        },

        getLoginState(val) { //监听用户登录状态
            if(val) {
                this.getUserInfo()
            }
        }
    },
    methods: {
        getUserInfo() {
            this.$http.get('/sys_user/getLoginUser')
                .then((res) => {
                    Storage.set('userRoleId', res.data.role.id)
                    Storage.set('userId', res.data.id)
                    this.$http.get('/sys_user/detail/' + res.data.id)
                        .then((res) => {
                            this.nickName = res.data.nickName
                            this.roleName = res.data.roleName
                            Storage.set('nickName', res.data.nickName)
                            Storage.set('roleName', res.data.roleName)
                            
                        })
                }) 
        },
        logoFn() { //点击logo
            this.$router.push({
                name: 'home'
            })
        },
        commandFn(command) { //顶部右侧操作
            switch(command) {
                case '1':
                    this.$router.push({
                        name: 'personalInfo'
                    })
                    break;
                case '2':
                    this.$router.push({
                        name: 'personalInfo',
                        query: {
                            editFlag: true
                        }
                    })
                    break;
                case '3':
                    this.$confirm('是否退出登录？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        this.$http.post('/sys_user/logout')
                            .then((res) => {
                                this.$message({
                                    type: 'success',
                                    message: '已退出!'
                                });
                                this.reload();
                                this.$store.commit("userLoginOut");
                                this.$router.push({
                                    name: 'login'
                                })
                            })
                    }).catch(() => {
                                    
                    });
                    break;
            }
        },
    },
}
</script>

<style lang="scss" scoped>
    .header-container {
        width: 100%;
        height: 60px;
        padding: 0 28px 0 16px;
        box-sizing: border-box;
        display: flex;
        justify-content: space-between;
        align-items: center;
        background-color: #4572CF;
        color: #fff;
        .logo-box {
            display: flex;
            align-items: center;
            font-size: 20px;
            cursor: pointer;
            img {
                margin-right: 10px;
            }
        }
        .user-box {
            display: flex;
            align-content: center;
            .user-name {
                padding-right: 10px;
                i {
                    position: relative;
                    margin-right: 4px;
                }
                span {
                    position: relative;
                }
            }
            .user-identity {
                position: relative;
                padding-left: 10px;
                i {
                    margin-left: 4px;
                }
                &::before {
                    content: "";
                    position: absolute;
                    top: 3px;
                    left: 0;
                    width: 2px;
                    height: 14px;
                    background-color: #fff;
                }
            }
        }
        .el-dropdown-link {
            cursor: pointer;
            color: #fff;
        }
        .el-icon-arrow-down {
            font-size: 12px;
        }
        .demonstration {
            display: block;
            color: #8492a6;
            font-size: 14px;
            margin-bottom: 20px;
        }
    }
</style>